package com.pragmatic.controller;

import com.alibaba.fastjson.JSON;
import com.pragmatic.cryptography.CallerSDKClient;
import com.pragmatic.domain.CommonRequest;
import com.pragmatic.domain.CommonResponse;
import com.pragmatic.domain.OrderInfo;
import com.pragmatic.domain.UserInfo;
import com.pragmatic.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 生产实际操作
 *
 * @author wangdong
 * @since 2022/9/28 11:19
 */
@Slf4j
@RestController
@RequestMapping("/generic")
public class GenericController {

  @Resource
  private CallerSDKClient callerSDKClient;

  /**
   * 查询用户信息
   *
   * @param userInfo 用户对象信息
   */
  @RequestMapping(value = "/findUser", method = RequestMethod.POST)
  public CommonResponse<UserInfo> getUserInfoCryptography(@RequestBody UserInfo userInfo) {
    // 组装CommonRequest对象
    CommonRequest<UserInfo> request =
        new CommonRequest<UserInfo>("http://localhost:7071/generic/getUserInfo")
            .setPayload(userInfo);
    try {
      log.info("caller===泛型===请求参数={}", JSON.toJSONString(request));
      CommonResponse<UserInfo> response = callerSDKClient.execute(request, UserInfo.class);
      log.info("caller===泛型===响应结果={}", JSON.toJSONString(response));
      // 返回结果（这里的结果集可以在某处判断各级code）
      return null;
    } catch (Exception e) {
      // 其他异常等
      throw new RuntimeException(e.getMessage());
    }
  }

  /**
   * 查询订单
   *
   * @param orderInfo 订单对象信息
   */
  @RequestMapping(value = "/findOrder", method = RequestMethod.POST)
  public CommonResponse<OrderInfo> getOrderInfoCryptography(@RequestBody OrderInfo orderInfo) {
    // 组装CommonRequest对象
    CommonRequest<OrderInfo> request =
            new CommonRequest<OrderInfo>("http://localhost:7071/generic/getOrderInfo")
                    .setPayload(orderInfo);
    try {
      log.info("caller===泛型===请求参数={}", JSON.toJSONString(request));
      CommonResponse<OrderInfo> response = callerSDKClient.execute(request, OrderInfo.class);
      log.info("caller===泛型===响应结果={}", JSON.toJSONString(response));
      // 返回结果（这里的结果集可以在某处判断各级code）
      return response;
    } catch (Exception e) {
      // 其他异常等
      throw new RuntimeException(e.getMessage());
    }
  }
}
